package com.bong.panpan.bongcharttools.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.bong.panpan.bongcharttools.R;
import com.bong.panpan.bongcharttools.custom.Constant;
import com.bong.panpan.bongcharttools.custom.MyUtil;

import java.io.File;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class UserListActivity extends Activity {

    @InjectView(R.id.list)
    ListView list;
    ArrayAdapter<String> userList;
    private File[] files;
    private int adviseNum = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_list);
        ButterKnife.inject(this);
        setTitle("用户列表");
        getActionBar().setDisplayHomeAsUpEnabled(true);
        userList = new ArrayAdapter<>(this, R.layout.list_item_user);
        list.setAdapter(userList);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //MyUtil.showToast(UserListActivity.this,""+position);
                Intent intent = new Intent();
                intent.putExtra("userName", userList.getItem(position));
                intent.putExtra("userPath", files[position].getPath());
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });
        files = new File(Constant.userStorePath).listFiles();
        String[] temp;
        int maxNum = 0;
        int tempNum;
        for (int i = 0; i < files.length; i++) {
            temp = files[i].getName().split("-");
            userList.add(temp[0] + "   " + temp[1]);
            tempNum = Integer.parseInt(temp[1], 10);
            if (tempNum > maxNum)
                maxNum = tempNum;
        }
        if (userList.getCount() == 0) {
            MyUtil.showToast(this, "用户列表为空\n请点击右上角“添加”按钮,添加用户。");
        }
        adviseNum = maxNum + 1;
        userList.notifyDataSetChanged();
        this.registerForContextMenu(list);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                int position;
                if ((position = list.getCheckedItemPosition()) != -1) {
                    Intent intent = new Intent();
                    intent.putExtra("userName", userList.getItem(position));
                    intent.putExtra("userPath", files[position].getPath());
                    setResult(Activity.RESULT_OK, intent);
                }
                finish();
                break;
            case R.id.action_add:
                Intent intent = new Intent(this, UserInfoActivity.class);
                intent.putExtra("title", "添加用户");
                intent.putExtra("adviseNum", adviseNum);
                startActivityForResult(intent, 0x01);
                //Toast.makeText(this, "action_add", Toast.LENGTH_SHORT).show();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.user_activity_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
                                    ContextMenu.ContextMenuInfo menuInfo) {
        AdapterView.AdapterContextMenuInfo menuInfo2 = (AdapterView.AdapterContextMenuInfo) menuInfo;
        menu.setHeaderTitle(userList.getItem(menuInfo2.position));
        menu.add(0, 0, Menu.NONE, "编辑").setIcon(R.mipmap.open);
        menu.add(0, 1, Menu.NONE, "删除").setIcon(R.mipmap.delete);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        final AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item
                .getMenuInfo();
        //MyUtil.showToast(UserListActivity.this,""+menuInfo.position);
        switch (item.getItemId()) {
            case 0:
                Intent intent = new Intent(this, UserInfoActivity.class);
                intent.putExtra("title", "修改用户");
                intent.putExtra("recordPath", files[menuInfo.position].getPath());
                startActivityForResult(intent, 0x01);
                break;
            case 1:
                MyUtil.showAlertDialog(this, "提示",
                        "确认删除？\n" + userList.getItem(menuInfo.position) + "\n删除后不可恢复，请谨慎操作。",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                files[menuInfo.position].delete();
                                userList.remove(userList.getItem(menuInfo.position));
                                userList.notifyDataSetChanged();
                                for (int i = menuInfo.position; i < userList.getCount(); i++) {
                                    files[i] = files[i + 1];
                                }
                            }
                        });
                break;
        }
        return super.onContextItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0x01) {
            if (resultCode == Activity.RESULT_OK) {
                if (data != null) {
                    setResult(Activity.RESULT_OK, data);
                    finish();
                } else {
                    userList.clear();
                    files = new File(Constant.userStorePath).listFiles();
                    String[] temp;
                    int tempNum;
                    int maxNum = 0;
                    for (int i = 0; i < files.length; i++) {
                        temp = files[i].getName().split("-");
                        userList.add(temp[0] + "   " + temp[1]);
                        tempNum = Integer.parseInt(temp[1], 10);
                        if (tempNum > maxNum)
                            maxNum = tempNum;
                    }
                    adviseNum = maxNum + 1;
                    userList.notifyDataSetChanged();
                }
            }
        }
    }

    @Override
    public void onBackPressed() {
        int position;
        if ((position = list.getCheckedItemPosition()) != -1) {
            Intent intent = new Intent();
            intent.putExtra("userName", userList.getItem(position));
            intent.putExtra("userPath", files[position].getPath());
            setResult(Activity.RESULT_OK, intent);
        }
        finish();
    }
}
